/**
 * @file      ac_tlm_mul.cpp
 * @author    Luisa Madeira Cardoso
 *
 */

//////////////////////////////////////////////////////////////////////////////
// Standard includes
// SystemC includes
// ArchC includes

#include "ac_tlm_mul.h"

//////////////////////////////////////////////////////////////////////////////

/// Namespace to isolate mul_mem from ArchC
using user::ac_tlm_mul;

/// Constructor
ac_tlm_mul::ac_tlm_mul( sc_module_name module_name ) :
  sc_module( module_name ),
  target_export("iport")
{
    /// Binds target_export to the mul_mem
    target_export( *this );
    /// Initialize multiplication mul_mem vector
    mul_mem = new uint8_t[48];
    for(int k=0;k<48;k++) mul_mem[k]=0;

}

/// Destructor
ac_tlm_mul::~ac_tlm_mul() {
}

/** Internal Write
  * Note: Always write 32 bits
  * @param a is the address to write
  * @param d id the data being write
  * @returns A TLM response packet with SUCCESS
*/
ac_tlm_rsp_status ac_tlm_mul::writel( const uint32_t &a , const uint32_t &d )
{
  *((uint32_t *) &mul_mem[a - 5242884]) = *((uint32_t *) &d);
  return SUCCESS;
}

/** Internal Read
  * Note: Always read 32 bits
  * @param a is the address to read
  * @param d id the data that will be read
  * @returns A TLM response packet with SUCCESS and a modified d
*/
ac_tlm_rsp_status ac_tlm_mul::readl( const uint32_t &a , uint32_t &d )
{
   mul();
  
  *((uint32_t *) &d) = *((uint32_t *) &mul_mem[a - 5242884]);
  return SUCCESS;
}

void ac_tlm_mul::mul()
{
  double d1, d2, d3, d4;
    d1 = *((double*)(&mul_mem[0]));
    d2 = *((double*)(&mul_mem[8]));
    *((double*)(&mul_mem[16])) = d1*d2;

    d3 = *((double*)(&mul_mem[24]));
    d4 = *((double*)(&mul_mem[32]));
    *((double*)(&mul_mem[40])) = d3*d4;
}

